package com.xxx.car.web.admin;

import com.xxx.car.entity.vo.request.ReqChargeGiveAdmin;
import com.xxx.car.service.TradeService;
import com.xxx.common.base.enums.AdminErrorEnum;
import com.xxx.common.base.vo.AdminResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * Created by xuwenjie on 2017/6/27.
 */
@Controller
@RequestMapping("/admin/chargeGive")
public class AdminChargeGiveController {

    @Autowired
    private TradeService tradeService;

    @RequestMapping("/")
    public String index()  {
        return "chargeGive";
    }

    @RequestMapping("/del")
    @ResponseBody
    public AdminResponse del(@RequestParam Integer id) {
        tradeService.delteChargeGive(id);
        return AdminResponse.returnSuccess();
    }

    @RequestMapping("/list")
    @ResponseBody
    public AdminResponse list(){
        return AdminResponse.returnSuccess(tradeService.getAllChargeGive());
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public AdminResponse saveOrUpdate(@Valid @ModelAttribute ReqChargeGiveAdmin reqChargeGiveAdmin, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            AdminResponse.returnFail(AdminErrorEnum.PARAMETER_INVALID);
        }
        tradeService.saveOrUpdateChargeGive(reqChargeGiveAdmin);
        return AdminResponse.returnSuccess();
    }
}
